<?php
/**
 * tis-tis 文章分类管理文件
 * 
 * @copyright (c)2010 昆明力行软件  保留所有权利。
 * @link http://www.tis-tis.com
 * @Author: XiangJie
 * @version: 1.0.0 
*/
require '../init.php';
require TIS_ROOT.'/admin/internal/smarty_config.php';
require TIS_ROOT.'/admin/internal/tools.php';
require TIS_ROOT.'/internal/common.php';
require_once TIS_ROOT.'/internal/mysql.class.php';

if (empty($_REQUEST['act']))
{
	$_REQUEST['act'] = 'list';
}

$DB = MySql::getInstance();
$smarty->assign('tpl_path','templates/default');
$cat = $_REQUEST['act'];
/*获取分类列表*/
if ($cat == 'list') {

	$articlecat = getArticleCategory(0);

	$smarty->assign("title", getPageTitle("文章分类列表"));
	$smarty->assign("articlecat", $articlecat);
	$smarty->display('article_category_list.tpl');
}

else if ($cat == 'add') {
	$smarty->assign('cat_select',  getArticleCategory(0));
	$smarty->assign('form_action', 'insert');
	$smarty->assign("title", getPageTitle("添加文章分类"));

	$smarty->display('article_category_info.tpl');
}

else if ($_REQUEST['act'] == 'insert') {
	$isUnique = isUnique($DB->table('article_category'), "cat_name", $_POST['cat_name']);

	if (!$isUnique)
	{
		$links[0] = "分类名称不能重复,点击<a href='article_category.php?act=add'>重新添加</a>。";
		tisMessage($links);
	}

	$sql = "INSERT INTO ".$DB->table('article_category')."(cat_name, cat_des, parent_id)
           VALUES ('$_POST[cat_name]',  '$_POST[cat_des]', '$_POST[parent_id]')";
	$DB->query($sql);


	redirectURL('article_category.php?act=list');
}

else if ($cat == 'edit') {
	$sql = "SELECT id, cat_name, cat_des, parent_id FROM ".
	$DB->table('article_category'). " WHERE id='$_REQUEST[id]'";
	$cat = $DB->GetRow($sql);
	$options = getArticleCategory(0);

	$selected   =   $cat['parent_id'];
	foreach ($options as $var)
	{
		if ($var['id'] == $_REQUEST['id'])
		{
			continue;
		}
		$select .= '<option value="' . $var['id'] . '" ';
		$select .= ($selected == $var['id']) ? "selected='ture'" : '';
		$select .= '>';
		if ($var['level'] > 0)
		{
			$select .= str_repeat('&nbsp;', $var['level'] * 4);
		}
		$select .= htmlspecialchars($var['cat_name']) . '</option>';
	}
	$smarty->assign('cat',         $cat);
	$smarty->assign('cat_select',  $select);
	$smarty->assign('form_action', 'update');
	$smarty->assign("title", getPageTitle("编辑文章分类"));

	$smarty->display('article_category_info.tpl');
}

else if ($_REQUEST['act'] == 'update') {

	/*检查重名*/
	if ($_POST['cat_name'] != $_POST['old_catname']){
		$isUnique = isUnique($DB->table('article_category'), "cat_name", $_POST['cat_name']);
		if (!$isUnique)
		{
			$links[0] = "分类名称不能重复,点击<a href='article_category.php?act=edit&id=".$_POST['id']."'>重新编辑</a>。";
			tisMessage($links);
		}
	}

	if(!isset($_POST['parent_id']))
	{
		$_POST['parent_id'] = 0;
	}
	$sql = "UPDATE ".$DB->table('article_category')." set cat_name='$_POST[cat_name]', cat_des='$_POST[cat_des]', parent_id='$_POST[parent_id]'
	     where id='$_POST[id]'";
	$DB->query($sql);

	redirectURL('article_category.php?act=list');
}

else if ($_REQUEST['act'] == 'delete') {
	$id = intval($_GET['id']);
	$sql = "SELECT COUNT(*) FROM " . $DB->table('article_category') . " WHERE parent_id = '$id'";
	if ($DB->getOne($sql) > 0){
		$links[0] = "对不起，删除失败，该分类下面还有子分类。";
		$links[1] = "<a href='article_category.php?act=list'>点击返回</a>";
		tisMessage($links);
	}
	
	$sql = "SELECT COUNT(*) FROM " . $DB->table('article') . " WHERE id = '$id'";
	if ($DB->getOne($sql) > 0){
		$links[0] = "对不起，删除失败，该分类下面还有文章。";
		$links[1] = "<a href='article_category.php?act=list'>点击返回</a>";
		tisMessage($links);
	}

	$sql = "DELETE from ".$DB->table('article_category')." where id='$id'";
	$DB->query($sql);

	redirectURL('article_category.php?act=list');

}